import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, ViewController } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UserInfoProvider } from '../../providers/user-info/user-info';
import { user_info } from '../login/login';
import { ComplainDataService } from '../../providers/service/complain-data.service';
import { Network } from '@ionic-native/network';


/**
 * Generated class for the ComplainRecordPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-complain-record',
  templateUrl: 'complain-record.html',
})
export class ComplainRecordPage {

  member_id: any;
  //页数
  page = 1;
  //用户信息
  userInfo: user_info = new user_info();
  //有网状态=1 断网=0
  Internet = 1;
// 下拉是否还有数据
  nodata = 0;

  constructor(
    public navCtrl: NavController,
    public viewCtrl: ViewController,
    public http: HttpProvider,
    public storage: StorageProvider,
    public dialog: DialogProvider,
    public complaindata: ComplainDataService,
    private network: Network,
    public user: UserInfoProvider) { }

  // public record = [
  //   {
  //     problem_type: null,
  //     remarks: null,
  //     created_at: null,
  //     representa_state: null
  //   }
  // ];

  public record: Record[] = new Array<Record>()

  async ionViewDidLoad() {
    // this.storage.getStorage("login_info").then(res => {
    //   if (res != undefined) {
    //     this.member_id = res['member_id']
    //     this.getRecord();
    //   }
    // });

    console.log('ionViewDidLoad ComplainRecordPage');
  }

  
  async ionViewDidEnter() {
      
    if (this.network.type !== 'none' && this.network.type !== '2g') {
      this.Internet = 1
      this.userInfo = this.user.getuserInfo();
      this.member_id = this.userInfo.member_id;
      this.getRecord();
    }
    if (this.network.type === 'none' || this.network.type === '2g') {
      this.Internet = 0
      let a = await this.complaindata.find((record) => record.key !== '');
      this.record = a;
    }
  }


  // 申诉记录
  getRecord() {
    this.http.postJson('', 'business/appeal/index?page=' + this.page, {
      "where": ["=", "member_id", this.member_id],
      "distinct": true,
      "limit": 5,
      "orderBy": "appeal_id DESC",
      "joinwith": ["refoundorder"]
    }, '').then(res => {
      console.log(res)
      if (res['status'] == 200) {
        this.getMessage(res['data']);
      }
      if(res['data'].length < 5){
	    	this.nodata = 1;
	  }
	  if(res['data'].length === 5){
	    	this.nodata = 0;
	  }
    });

  }

  async getMessage(record) {
    this.record.splice(0);
    for (let rec of record) {
      if (rec) {
        var a = {
          problem_type: rec.problem_type,
          remarks: rec.remarks,
          order_code:rec.order_code,
          created_at: rec.created_at * 1000,
          representa_state: rec.representa_state,
          refoundorder: rec.refoundorder,
        };
        await this.record.push(a);
      }
    }
    this.complaindata.deleteAll();
    for (var i = 0; i < this.record.length; i++) {
      this.complaindata.save(this.record[i]);
    }
  }



  //无限滚动(下滑加载)
  async  lodeMessage(infiniteScroll, type) {
    this.page++;
    const res = await this.http.postJson('', 'business/appeal/index?page=' + this.page,
      {
        "where": ["=", "member_id", this.member_id],
        "distinct": true,
        "limit": 5,
        "orderBy": "appeal_id DESC",
        "joinwith": ["refoundorder"]
      }, '').then(res => {
        if (res['status'] == 200) {
          
	      if (res['data'].length === 0) {
				this.nodata = 1;
		  }
        	
          for (let rec of res['data']) {
            if (rec) {
              var a = {
                problem_type: rec.problem_type,
                remarks: rec.remarks,
                order_code:rec.order_code,
                created_at: rec.created_at * 1000,
                representa_state: rec.representa_state,
                refoundorder: rec.refoundorder,
              };
              this.record.push(a);
            }
          }
          if (type === 'scroll') {
				setInterval(infiniteScroll.complete(), 500);
		  }
        }
      });
  }


  getproblem(problem) {
    if (problem == 1) {
      return '充电异常';
    }
    if (problem == 2) {
      return '收费异常';
    }
    if (problem == 3) {
      return '设备异常';
    }


  }
  getstate(state) {
    if (state == 0) {
      return '未处理';
    }
    if (state == 1) {
      return '处理完成';
    }
  }

    getRefoun(data) {
    if (data.representa_state == 1) {
      let state;
      let money = data.refoundorder.refund_money;
      if (data.refoundorder.refund_state == 0) {
        return '拒绝退款';
      }
      if (data.refoundorder.refund_state == 1) {
        return '全额退款: ' + money + '元';
      }
      if (data.refoundorder.refund_state == 2) {
        return '部分退款: ' + money + '元';
      }
    }
  }
}



export class Record {

  problem_type: any;
  remarks: any;
  order_code:any;
  created_at: any;
  representa_state: any;
}
